﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall.Screens;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Kristall.Engine;

namespace Kristall.Editor.Commands
{
    public class PasteCommand : ICommand
    {
        public AABB Pasted
        {
            get
            {
                return _pasted;
            }
        }

        AABB _pasted;
        private Microsoft.Xna.Framework.Vector2 _pasteCoords;

        public PasteCommand(Level level, Vector2 _pasteCoords)
        {
            this._pasteCoords = _pasteCoords;

            var data = Clipboard.GetDataObject().GetData(ToolPalette.AnimatableDataFormat.Name);

            if (data != null)
            {
                MemoryStream ms = new MemoryStream((byte[])data);
                XmlSerializer serializer = new XmlSerializer(typeof(AABB), Kristall.GetExtraTypes());
                AABB entity = (AABB)serializer.Deserialize(ms);

                if (entity != null)
                {
                    _pasted = entity;
                    _pasted.LoadContent(level);
                    _pasted.Position = _pasteCoords;
                }
            }
        }

        public bool Execute()
        {
            if (_pasted != null)
            {
                _pasted.AddToLevel();
                return true;
            }

            return false;
        }

        public bool Undo()
        {
            _pasted.RemoveFromLevel();
            return true;
        }


        public string Name
        {
            get { return "Paste"; }
        }
    }
}
